package com.xmad.ksadutils.AdSDK.AdMold.OnCallback;

import android.app.Activity;
import android.os.Handler;
import android.util.Log;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsAdVideoPlayConfig;
import com.kwad.sdk.api.KsFeedAd;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsScene;
import com.xmad.ksadutils.AdSDK.Interface.AdOnLoad;

import java.util.List;

public class KsLoadFeedAd implements AdOnLoad {
    @Override
    public void noCallback(Long id, Activity activity, FrameLayout frameLayout) {
        Log.e("noCallback",activity.toString()+id+"");
        KsScene scene = new KsScene.Builder(id).adNum(1).build();
        KsAdSDK.getLoadManager()
                .loadFeedAd(scene, new KsLoadManager.FeedAdListener() {
                    @Override
                    public void onError(int code, String msg) {
                        Log.e("hjw", code+"===="+msg);
                    }

                    @Override
                    public void onFeedAdLoad(@Nullable List<KsFeedAd> adList) {
                        if (adList == null || adList.isEmpty()) {
                            Log.e("TAG", "广告数据为空");
                            return;
                        }
                        for (KsFeedAd ksFeedAd : adList) {
                            if (ksFeedAd == null) {
                                continue;
                            }
                            KsAdVideoPlayConfig videoPlayConfig = new KsAdVideoPlayConfig.Builder().videoSoundEnable(true) // 是否有声播放
                                    .dataFlowAutoStart(false) // 是否非WiFi下自动播放
                                    .build();
                            ksFeedAd.setVideoPlayConfig(videoPlayConfig);
                            KsFeedAd  Ad=adList.get(0);
                                frameLayout.removeAllViews();
                                frameLayout.addView(Ad.getFeedView(activity));
                                frameLayout.setPadding(5,5,5,5);
                                ksFeedAd.setAdInteractionListener(new KsFeedAd.AdInteractionListener() {
                                    @Override
                                    public void onAdClicked() {
                                        Log.e("hjw", "onAdClicked");
                                    }

                                    @Override
                                    public void onAdShow() {
                                        Log.e("hjw", "onAdShow");
                                        new Handler().postDelayed(new Runnable() {
                                            @Override
                                            public void run() {
                                                frameLayout.removeAllViews();
                                            }
                                        },1000);
                                    }

                                    @Override
                                    public void onDislikeClicked() {
                                        Log.e("TAG", "onDislikeClicked");
                                        adList.remove(ksFeedAd);
                                    }

//                                    @Override
//                                    public void onDownloadTipsDialogShow() {
//
//                                    }
//
//                                    @Override
//                                    public void onDownloadTipsDialogDismiss() {
//
//                                    }
                                });

                        }
                    }
                });
    }

    @Override
    public void noCallback(String id, Activity activity, FrameLayout frameLayout) {

    }
}
